<template>
  <div id="view-range">
    <div class="tableControl m15">
      <el-button type="primary" icon="el-icon-plus" @click="addrange">添加权限</el-button>
      <el-button type="primary" icon="el-icon-minus" @click="batchDelrange" :disabled="!selection.length">批量删除</el-button>
    </div>
    <div class="rangeList m15">
      <table-field :config="config" ref="rangeList" @selection-change="selectionChange">
        <el-table-column type="selection" width="55" fixed="left" align="center">
        </el-table-column>
        <el-table-column label="权限名称" prop="rangeName"></el-table-column>
        <el-table-column label="菜单权限" prop="routerLevel" align="center"></el-table-column>
        <el-table-column label="创建时间" prop="createTime" min-width="180px" align="center"></el-table-column>
        <el-table-column label="更新时间" prop="updateTime" min-width="180px" align="center"></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" min-width="120px">
          <template slot-scope="scope">
            <el-tooltip content="编辑">
              <el-button class="el-icon-edit" @click="handleEdit(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除">
              <el-button class="el-icon-delete" @click="handleDel(scope.row)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </table-field>
    </div>
    <drawer-field :visible.sync="isShowDrawer" :title="drawerTitle" @confirm="confirm">
      <el-form :model="rangeData" :rules="rules" ref="rangeForm" label-width="auto" class="" :key="key">
        <el-form-item label="权限名称" prop="rangeName">
          <el-input v-model="rangeData.rangeName" @input="change($event)"></el-input>
        </el-form-item>
        <el-form-item label="菜单权限" prop="routerLevel">
          <el-input v-model.number="rangeData.routerLevel"></el-input>
        </el-form-item>
      </el-form>
    </drawer-field>
  </div>
</template>

<script>
import { addRangeApi, updateRangeApi, deleteRangeApi } from '@/api/range'
import TableField from '@/components/TableField'
import DrawerField from '@/components/DrawerField'
import { deepClone } from '@/utils/common'

export default {
  name: 'view-range',
  components: {
    [TableField.name]: TableField,
    [DrawerField.name]: DrawerField
  },
  data() {
    return {
      config: {
        url: '/range/list',
        rootProperty: 'list',
        data: {
        }
      },
      isShowDrawer: false,
      drawerTitle: '',
      isEdit: false,
      key: 0,
      selection: [],
      rules: {
        name: [
          { required: true, message: '请输入权限名', trigger: 'change' }
        ]
      },
      rangeData: {}
    }
  },
  watch: {
    isShowDrawer(newVal) {
      newVal === false || this.key++
    }
  },
  methods: {
    getrangeList(isReload) {
      this.$refs.rangeList.loadData(isReload)
    },
    addrange() {
      this.isShowDrawer = true
      this.isEdit = false
      this.drawerTitle = '添加权限'
      this.rangeDataInit()
    },
    batchDelrange() {
      this.handleDel(this.selection)
    },
    change() {
      this.$forceUpdate()
    },
    confirm() {
      if (!this.isEdit) {
        addRangeApi(this.rangeData).then(res => {
          if (res.code === 1) {
            this.$message({
              type: 'success',
              message: '新建权限成功!'
            })
          } else {
            this.$message({
              type: 'error',
              message: res.msg
            })
          }
          this.getrangeList()
          this.isShowDrawer = false
          this.rangeDataInit()
        })
          .catch(() => {
            this.isShowDrawer = false
            this.rangeDataInit()
          })
      } else {
        updateRangeApi(this.rangeData).then(res => {
          if (res.code === 1) {
            this.getrangeList()
          }
          this.isShowDrawer = false
          this.rangeDataInit()
        })
          .catch(() => {
            this.isShowDrawer = false
            this.rangeDataInit()
          })
      }
    },
    handleEdit(row) {
      this.isShowDrawer = true
      this.isEdit = true
      this.drawerTitle = '编辑权限'
      this.rangeData = deepClone(row)
    },
    handleDel(row) {
      let title = ''
      let params = null
      console.log(row)
      if (Array.isArray(row)) {
        params = row
        title += row.map(ele => ele.name).join(',')
      } else {
        params = [row]
        title = row.name
      }
      this.$confirm(`将删除${title}?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteRangeApi(params).then(res => {
          this.$message({
            type: 'success',
            message: '删除权限成功!'
          })
          this.getrangeList()
        })
          .catch(() => {
            this.$message({
              type: 'error',
              message: '删除权限失败!'
            })
            this.getrangeList()
          })
      }).catch(err => {
        console.log(err)
      })
    },
    selectionChange(selection) {
      this.selection = selection
    },
    rangeDataInit() {
      this.rangeData = {
        sex: 0,
        status: 1,
        isSuperAdmin: 0,
        isAdmin: 0
      }
    }
  },
  mounted() {
    this.getrangeList(true)
  }
}
</script>

<style lang="scss" scoped>
#view-range {
  .tableControl {
    display: flex;
  }

  .rangeList {}
}
</style>
